package com.cz.nongyetongv5.activity;


import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.bean.City;
import com.cz.nongyetongv5.bean.CityBean;
import com.cz.nongyetongv5.bean.JsonUtil;
import com.cz.nongyetongv5.bean.RegisterDetailBean;
import com.cz.nongyetongv5.bean.RegisterList;
import com.cz.nongyetongv5.bean.RegisterListBean;
import com.cz.nongyetongv5.bean.RegisterListResponse;
import com.cz.nongyetongv5.bean.StayMarkBean;
import com.cz.nongyetongv5.bean.User;
import com.cz.nongyetongv5.custom.dialog.CustomMyDialog;
import com.cz.nongyetongv5.fragment.AreaPopRegisterNewFragment;
import com.cz.nongyetongv5.fragment.FragmentAdapter;
import com.google.gson.Gson;
import com.ronri.common.WaitDialog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class RegisterListNewActivity extends FragmentActivity implements View.OnClickListener {
    private RelativeLayout rl_back;
    private TextView tv_right;

//    private List<PopBean> types;
//    private TypeAdapter typeAdapter;
//    private LinearLayout ll_type;
//    private TextView tv_type;
//    private LinearLayout ll_type_list;
//    private RecyclerView rv_type;
//    private LinearLayout ll_alpha;
    private RecyclerView rv_list;

    private LinearLayout ll_bottom;
    private TextView tv_pass;
    private TextView tv_refuse;

    private String add_user;
    private String region_code;

    private boolean isShowEdit = false;
    private int page = 1;
    private int size = 10;
    private UserListAdapter adapter;

    SmartRefreshLayout refreshLayout;
    private String ids = "";


    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    //ViewPage选项卡页面集合
    private List<Fragment> mFragments;
    private FragmentAdapter mFragmentAdapter;
    //Tab标题集合
    private List<String> mTitles;
    private RelativeLayout ll_area_pop;
    private TextView tv_confirm;
    private LinearLayout ll_area;
    private TextView tv_area;
    private User user;

    private String code;
    private String name = "";

    private CheckBox cb_choose;
    private LinearLayout ll_choose;

    private TextView tv_count;

    private StayMarkBean stayMarkBean;


    private RegisterListResponse registerListResponse;
    private RegisterListBean registerListBean;
    private List<RegisterList> registerLists;

    private String type = "0";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_list_new);
        user = (User) getIntent().getSerializableExtra("user");
        add_user = user.getUsername();
        region_code = user.getKnowRegion();
        code = user.getKnowRegion();
        name = user.getRegion_name();
//        types = new ArrayList<>();

        mTitles = new ArrayList<>();
        mFragments = new ArrayList<>();
//        types.add(new PopBean("主体对象巡查", false, "1"));
//        types.add(new PopBean("其他巡查", false, "2"));
//        types.add(new PopBean("全部", true, "0"));
        rl_back = findViewById(R.id.rl_back);
//        ll_type_list = findViewById(R.id.ll_type_list);
//        ll_alpha = findViewById(R.id.ll_alpha);
        rl_back.setOnClickListener(this);
        tv_right = findViewById(R.id.tv_right);
        tv_right.setText("批量处理");
        tv_right.setOnClickListener(this);
//        tv_type = findViewById(R.id.tv_type);
//        tv_type.setText("全部");
//        rv_type = findViewById(R.id.rv_type);
        ll_bottom = findViewById(R.id.ll_bottom);
        tv_pass = findViewById(R.id.tv_pass);
        tv_refuse= findViewById(R.id.tv_refuse);
        tv_pass.setOnClickListener(this);
        tv_refuse.setOnClickListener(this);
//        typeAdapter = new TypeAdapter();
//        rv_type.setLayoutManager(new LinearLayoutManager(UserListNewActivity.this));
//        rv_type.setAdapter(typeAdapter);
//        ll_alpha.setOnClickListener(this);
//        typeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                for (PopBean popupBean : types) {
//                    popupBean.setChecked(false);
//                }
//                types.get(position).setChecked(true);
//                adapter.notifyDataSetChanged();
//                tv_type.setText(types.get(position).getName());
//                //                rv_year.setVisibility(View.GONE);
//                ll_type_list.setVisibility(View.GONE);
//                tv_type.setTextColor(Color.parseColor("#585858"));
//                tv_type.setCompoundDrawablesWithIntrinsicBounds(0,
//                        0, R.drawable.jt_down_black, 0);
//                type = types.get(position).getLacationTag();
//                page = 1;
//                WaitDialog.ShowDialog(UserListNewActivity.this, "数据加载中...");
//                getPatrolList();
//            }
//        });
//        ll_type = findViewById(R.id.ll_type);
//        ll_type.setOnClickListener(this);
        rv_list = findViewById(R.id.rv_list);
        refreshLayout = findViewById(R.id.refreshLayout);
        registerLists = new ArrayList<>();
        adapter = new UserListAdapter();
        View notDataView = getLayoutInflater().inflate(R.layout.view_nodata, (ViewGroup) rv_list.getParent(), false);
        adapter.setEmptyView(notDataView);
        rv_list.setLayoutManager(new LinearLayoutManager(RegisterListNewActivity.this));
        rv_list.setAdapter(adapter); //设置分页和下拉刷新
        refreshLayout.setDisableContentWhenRefresh(true);
        refreshLayout.setDisableContentWhenLoading(true);
        refreshLayout.setRefreshHeader(new ClassicsHeader(RegisterListNewActivity.this));
        //设置 Footer
        refreshLayout.setRefreshFooter(new ClassicsFooter(RegisterListNewActivity.this));
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                WaitDialog.ShowDialog(RegisterListNewActivity.this, "数据加载中...");
                getRegisterList();
                //                WaitDialog.ShowDialog(getActivity(),"数据加载中...");

            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (registerListBean != null) {
                    if (page < registerListBean.getTotal()) {
                        page++;
                        WaitDialog.ShowDialog(RegisterListNewActivity.this, "数据加载中...");
                        getRegisterList();
                    } else {
                        refreshLayout.finishLoadmore();
                        Toast.makeText(RegisterListNewActivity.this, "没有更多数据", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    refreshLayout.finishLoadmore();
                    Toast.makeText(RegisterListNewActivity.this, "没有数据", Toast.LENGTH_SHORT).show();
                }
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                WaitDialog.ShowDialog(RegisterListNewActivity.this, "数据加载中...");
                getRegisterDetail(registerLists.get(position).getId());
            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.ll_root || view.getId() == R.id.rl_ent) {
                    WaitDialog.ShowDialog(RegisterListNewActivity.this, "数据加载中...");
                                    getRegisterDetail(registerLists.get(position).getId());
                } else {
                    if (registerLists.get(position).isChecked()) {
                        registerLists.get(position).setChecked(false);
                    } else {
                        registerLists.get(position).setChecked(true);
                    }
                    adapter.notifyDataSetChanged();
                    int sum = 0;
                    for (RegisterList registerList : registerLists) {
                        if (registerList.isChecked()) {
                            sum++;
                        }
                    }
                    if (sum == registerLists.size()) {
                        cb_choose.setChecked(true);
                    } else {
                        cb_choose.setChecked(false);
                    }
                    if (stayMarkBean != null) {
                        if (sum == 0) {
                            SpannableString ss = new SpannableString("待审核" + String.valueOf(stayMarkBean.getData().getRegisterCount()) + "户");
                            ss.setSpan(new ForegroundColorSpan(Color.parseColor("#ff3d5d")), 3, 3 + String.valueOf(stayMarkBean.getData().getRegisterCount()).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            tv_count.setText(ss);
                        } else {
                            SpannableString ss = new SpannableString("待审核" + String.valueOf(stayMarkBean.getData().getRegisterCount()) + "户,已选择" + String.valueOf(sum) + "户");
                            ss.setSpan(new ForegroundColorSpan(Color.parseColor("#ff3d5d")), 3, 3 + String.valueOf(stayMarkBean.getData().getRegisterCount()).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            ss.setSpan(new ForegroundColorSpan(Color.parseColor("#ff3d5d")), 8 + String.valueOf(stayMarkBean.getData().getRegisterCount()).length(), 8 + String.valueOf(stayMarkBean.getData().getRegisterCount()).length() + String.valueOf(sum).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            tv_count.setText(ss);
                        }
                    } else {
                        if (sum == 0) {
                            tv_count.setText("暂无数据");
                        } else {
                            SpannableString ss = new SpannableString("已选择" + String.valueOf(sum) + "户");
                            ss.setSpan(new ForegroundColorSpan(Color.parseColor("#ff3d5d")), 3, 3 + String.valueOf(sum).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            tv_count.setText(ss);
                        }
                    }
                }

            }
        });

        ll_area = findViewById(R.id.ll_area);
        ll_area.setOnClickListener(this);
        tv_area = findViewById(R.id.tv_area);
        tv_area.setText(user.getRegion_name());
        ll_area_pop = findViewById(R.id.ll_area_pop);
        mViewPager = findViewById(R.id.viewpager);
        mTabLayout = findViewById(R.id.tab);
        tv_confirm = findViewById(R.id.tv_confirm);
        tv_confirm.setOnClickListener(this);


        cb_choose = findViewById(R.id.cb_choose);
        ll_choose = findViewById(R.id.ll_choose);
        ll_choose.setOnClickListener(this);


        tv_count = findViewById(R.id.tv_count);
        WaitDialog.ShowDialog(RegisterListNewActivity.this, "数据加载中...");
        getRegisterList();
        getStay();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.ll_choose:
                if (cb_choose.isChecked()) {
                    cb_choose.setChecked(false);
                    setSelect(false);
                } else {
                    cb_choose.setChecked(true);
                    setSelect(true);
                }
                int sum = 0;
                for (RegisterList registerList : registerLists) {
                    if (registerList.isChecked()) {
                        sum++;
                    }
                }
                if (sum == registerLists.size()) {
                    cb_choose.setChecked(true);
                } else {
                    cb_choose.setChecked(false);
                }
                if (stayMarkBean != null) {
                    if (sum == 0) {
                        SpannableString ss = new SpannableString("待审核" + String.valueOf(stayMarkBean.getData().getRegisterCount()) + "户");
                        ss.setSpan(new ForegroundColorSpan(Color.parseColor("#ff3d5d")), 3, 3 + String.valueOf(stayMarkBean.getData().getRegisterCount()).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                        tv_count.setText(ss);
                    } else {
                        SpannableString ss = new SpannableString("待审核" + String.valueOf(stayMarkBean.getData().getRegisterCount()) + "户,已选择" + String.valueOf(sum) + "户");
                        ss.setSpan(new ForegroundColorSpan(Color.parseColor("#ff3d5d")), 3, 3 + String.valueOf(stayMarkBean.getData().getRegisterCount()).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                        ss.setSpan(new ForegroundColorSpan(Color.parseColor("#ff3d5d")), 8 + String.valueOf(stayMarkBean.getData().getRegisterCount()).length(), 8 + String.valueOf(stayMarkBean.getData().getRegisterCount()).length() + String.valueOf(sum).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                        tv_count.setText(ss);
                    }
                } else {
                    if (sum == 0) {
                        tv_count.setText("暂无数据");
                    } else {
                        SpannableString ss = new SpannableString("已选择" + String.valueOf(sum) + "户");
                        ss.setSpan(new ForegroundColorSpan(Color.parseColor("#ff3d5d")), 3, 3+ String.valueOf(sum).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                        tv_count.setText(ss);
                    }
                }
                break;
            case R.id.ll_alpha:
//                if (ll_type_list.getVisibility() == View.VISIBLE) {
//                    ll_type_list.setVisibility(View.GONE);
//                    tv_type.setTextColor(Color.parseColor("#585858"));
//                    tv_type.setCompoundDrawablesWithIntrinsicBounds(0,
//                            0, R.drawable.jt_down_black, 0);
//                }
                break;
            case R.id.tv_right:
                if (ll_area_pop.getVisibility() == View.VISIBLE) {
                    ll_area_pop.setVisibility(View.GONE);
                    tv_area.setTextColor(Color.parseColor("#585858"));
                    tv_area.setCompoundDrawablesWithIntrinsicBounds(0,
                            0, R.drawable.jt_down_black, 0);
                }
//                if (ll_type_list.getVisibility() == View.VISIBLE) {
//                    ll_type_list.setVisibility(View.GONE);
//                    tv_type.setTextColor(Color.parseColor("#585858"));
//                    tv_type.setCompoundDrawablesWithIntrinsicBounds(0,
//                            0, R.drawable.jt_down_black, 0);
//                }

                if (registerLists != null && registerLists.size() == 0) {
                    Toast.makeText(RegisterListNewActivity.this, "没有数据", Toast.LENGTH_SHORT).show();
                } else {
                    if (ll_bottom.getVisibility() == View.VISIBLE) {
                        isShowEdit = false;
                        ll_bottom.setVisibility(View.GONE);
                        tv_right.setText("批量处理");
                    } else {
                        isShowEdit = true;
                        ll_bottom.setVisibility(View.VISIBLE);
                        tv_right.setText("取消");
                    }
                    cb_choose.setChecked(false);
                    setSelect(false);
                    adapter.notifyDataSetChanged();
                }
                if (stayMarkBean != null) {
                    SpannableString ss = new SpannableString("待审核" + String.valueOf(stayMarkBean.getData().getRegisterCount()) + "户");
                    ss.setSpan(new ForegroundColorSpan(Color.parseColor("#ff3d5d")), 3, 3 + String.valueOf(stayMarkBean.getData().getRegisterCount()).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    tv_count.setText(ss);
                } else {
                    tv_count.setText("暂无数据");
                }
                break;
            case R.id.ll_type:
                if (ll_area_pop.getVisibility() == View.VISIBLE) {
                    ll_area_pop.setVisibility(View.GONE);
                    tv_area.setTextColor(Color.parseColor("#585858"));
                    tv_area.setCompoundDrawablesWithIntrinsicBounds(0,
                            0, R.drawable.jt_down_black, 0);
                }
//                if (ll_type_list.getVisibility() == View.GONE) {
//                    ll_type_list.setVisibility(View.VISIBLE);
//                    tv_type.setTextColor(Color.parseColor("#21b481"));
//                    tv_type.setCompoundDrawablesWithIntrinsicBounds(0,
//                            0, R.drawable.jt_up_green, 0);
//                } else {
//                    ll_type_list.setVisibility(View.GONE);
//                    tv_type.setTextColor(Color.parseColor("#585858"));
//                    tv_type.setCompoundDrawablesWithIntrinsicBounds(0,
//                            0, R.drawable.jt_down_black, 0);
//                }
                break;
            case R.id.tv_pass:
                ids = "";
                List<RegisterList> lists = getSelected();
                if (lists.size() <= 0) {
                    Toast.makeText(RegisterListNewActivity.this, "没有选择数据", Toast.LENGTH_SHORT).show();
                } else {
                    for (int i = 0; i < lists.size(); i++) {
                        if (i == 0) {
                            ids = lists.get(i).getId();
                        } else {
                            ids = ids + "," + lists.get(i).getId();
                        }
                    }
                    CustomMyDialog.Builder builder = new CustomMyDialog.Builder(RegisterListNewActivity.this);
                    builder.setMessage("确认提交？");
                    builder.setPositiveButton("确认提交", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            WaitDialog.ShowDialog(RegisterListNewActivity.this, "数据加载中...");
                            postCheckRegister("3", ids, "", add_user, region_code);
                        }
                    });
                    builder.setNegativeButton("下次再说", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    builder.createNormalDialog().show();
                }
                break;
            case R.id.tv_refuse:
                ids= "";
                List<RegisterList> lists1 = getSelected();
                if (lists1.size() <= 0) {
                    Toast.makeText(RegisterListNewActivity.this, "没有选择数据", Toast.LENGTH_SHORT).show();
                } else {
                    for (int i = 0; i < lists1.size(); i++) {
                        if (i == 0) {
                            ids= lists1.get(i).getId();
                        } else {
                            ids = ids + "," + lists1.get(i).getId();
                        }
                    }
                    Intent intent = new Intent(RegisterListNewActivity.this, DialogActivity.class);
                    intent.putExtra("add_user", add_user);
                    intent.putExtra("region_code", region_code);
                    intent.putExtra("ids", ids);
                    intent.putExtra("type", "2");
                    intent.putExtra("model", "register");
                    startActivityForResult(intent, 9021);
                }
                break;
            case R.id.ll_area:
//                if (ll_type_list.getVisibility() == View.VISIBLE) {
//                    ll_type_list.setVisibility(View.GONE);
//                    tv_type.setTextColor(Color.parseColor("#585858"));
//                    tv_type.setCompoundDrawablesWithIntrinsicBounds(0,
//                            0, R.drawable.jt_down_black, 0);
//                }
                if (ll_area_pop.getVisibility() == View.GONE) {
                    mTitles.clear();
                    mFragments.clear();
                    mTitles.add(user.getRegion_name());
                    List<City> cities = new ArrayList<>();
                    City city = new City();
                    city.setCode(user.getKnowRegion());
                    city.setName(user.getRegion_name());
                    city.setChecked(true);
                    cities.add(city);
                    mFragments.add(AreaPopRegisterNewFragment.newInstance(cities));
                    tv_area.setTextColor(Color.parseColor("#21b481"));
                    tv_area.setCompoundDrawablesWithIntrinsicBounds(0,
                            0, R.drawable.jt_up_green, 0);

                    //                    if (user.getKnowRegion().length() == 8) {
                    //                        initTablayout();
                    //                    } else {
                    WaitDialog.ShowDialog(RegisterListNewActivity.this, "数据加载中...");
                    getCity(user.getKnowRegion());
                    //                    }
                } else {
                    ll_area_pop.setVisibility(View.GONE);
                    tv_area.setTextColor(Color.parseColor("#585858"));
                    tv_area.setCompoundDrawablesWithIntrinsicBounds(0,
                            0, R.drawable.jt_down_black, 0);
                }
                break;

            case R.id.tv_confirm:
                for (int i = mFragments.size() - 1; i >= 0; i--) {
                    if (((AreaPopRegisterNewFragment) mFragments.get(i)).getSelectCity() != null) {
                        tv_area.setText(((AreaPopRegisterNewFragment) mFragments.get(i)).getSelectCity().getName());
                        code = ((AreaPopRegisterNewFragment) mFragments.get(i)).getSelectCity().getCode();
                        name = ((AreaPopRegisterNewFragment) mFragments.get(i)).getSelectCity().getName();
                        tv_area.setTextColor(Color.parseColor("#585858"));
                        tv_area.setCompoundDrawablesWithIntrinsicBounds(0,
                                0, R.drawable.jt_down_black, 0);
                        ll_area_pop.setVisibility(View.GONE);
                        page = 1;
                        WaitDialog.ShowDialog(RegisterListNewActivity.this, "数据加载中...");
                        getRegisterList();
                        return;
                    }
                }
                break;

            default:
                break;
        }
    }

    public void setSelect(boolean isSelect) {
        for (RegisterList registerLists : registerLists) {
            registerLists.setChecked(isSelect);
        }
        adapter.notifyDataSetChanged();
    }

//    public class TypeAdapter extends BaseQuickAdapter<PopBean, BaseViewHolder> {
//        public TypeAdapter() {
//            super(R.layout.status_pop_item, types);
//        }
//
//        @Override
//        protected void convert(BaseViewHolder helper, PopBean item) {
//            if (item.isChecked()) {
//                helper.setTextColor(R.id.tv_name, Color.parseColor("#21b481"));
//            } else {
//                helper.setTextColor(R.id.tv_name, Color.parseColor("#585858"));
//            }
//            helper.setText(R.id.tv_name, item.getName());
//        }
//    }


    void getRegisterList() {
        OkHttpUtils
                .get()
                .url(InitApplication.markUrl + "register/checkRegisterList.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("page", String.valueOf(page))
                .addParams("size", String.valueOf(size))
                .addParams("region_code", code)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        closeRefresh();
                        WaitDialog.HideDialog();
                        Toast.makeText(RegisterListNewActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        //                        System.out.println(response);
                        WaitDialog.HideDialog();
                        closeRefresh();
                        registerListResponse = JsonUtil.toObject(response, RegisterListResponse.class);
                        if (registerListResponse.getCode() == 0) {
                            registerListBean = registerListResponse.getData();
                            if (page == 1) {
                                registerLists.clear();
                                registerLists.addAll(registerListBean.getRows());
                            } else {
                                registerLists.addAll(registerListBean.getRows());
                            }
                            if (registerLists.size() == 0) {
                                isShowEdit = false;
                                tv_right.setText("批量处理");
                                ll_bottom.setVisibility(View.GONE);
                                adapter.notifyDataSetChanged();
                            } else {
                                int sum = 0;
                                for (RegisterList registerLists : registerLists) {
                                    if (registerLists.isChecked()) {
                                        sum++;
                                    }
                                }
                                if (sum == registerLists.size()) {
                                    cb_choose.setChecked(true);
                                } else {
                                    cb_choose.setChecked(false);
                                }
                                adapter.notifyDataSetChanged();
                            }
                        } else {
                            Toast.makeText(RegisterListNewActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }


    public class UserListAdapter extends BaseQuickAdapter<RegisterList, BaseViewHolder> {
        public UserListAdapter() {
            super(R.layout.user_list_item, registerLists);
        }

        @Override
        protected void convert(BaseViewHolder helper, RegisterList item) {
            if (item.getName()!= null) {
                helper.setText(R.id.tv_name, item.getName());
            }
            if (item.getRegion_name() != null) {
                helper.setText(R.id.tv_area, item.getRegion_name());
            }
            if (item.getAdd_time() != null) {
                helper.setText(R.id.tv_date, item.getAdd_time());
            }
            if (isShowEdit) {
                helper.setVisible(R.id.cb_choose, true);
            } else {
                helper.setVisible(R.id.cb_choose, false);
            }
            helper.addOnClickListener(R.id.cb_choose);
            helper.setChecked(R.id.cb_choose, item.isChecked());
        }
    }

    public void closeRefresh() {
        if (refreshLayout != null) {
            if (refreshLayout.isRefreshing()) {
                refreshLayout.finishRefresh();
            } else if (refreshLayout.isLoading()) {
                refreshLayout.finishLoadmore();
            }
        }
    }


    void getRegisterDetail(String id) {
        OkHttpUtils
                .get()
                .url(InitApplication.markUrl + "register/checkRegisterDetail.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("id", id)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        WaitDialog.HideDialog();
                        Toast.makeText(RegisterListNewActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        //                        System.out.println(response);
                        WaitDialog.HideDialog();
                        Gson gson = new Gson();
                        RegisterDetailBean registerDetailBean = gson.fromJson(response, RegisterDetailBean.class);
                        //                        EntDetailBean entDetailBean = JsonUtil.toObject(response, EntDetailBean.class);
                        if (registerDetailBean.getCode() == 0) {
                            if (registerDetailBean.getData() != null) {
                                Intent intent = new Intent(RegisterListNewActivity.this, RegisterDetailActivity.class);
                                intent.putExtra("add_user", add_user);
                                intent.putExtra("region_code", region_code);
                                intent.putExtra("bean", registerDetailBean.getData());
                                startActivityForResult(intent, 9021);
                            } else {
                                Toast.makeText(RegisterListNewActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(RegisterListNewActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }


    /**
     * 获取选中的item
     *
     * @return
     */
    public List<RegisterList> getSelected() {
        List<RegisterList> selectedItems = new ArrayList<>();
        for (RegisterList registerList : registerLists) {
            if (registerList.isChecked()) {
                selectedItems.add(registerList);
            }
        }
        return selectedItems;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 9021:
                    page = 1;
                    //                    editType = 1;
                    //                    isShowEdit = false;
                    //                    tv_right.setText("审核");
                    //                    ll_bottom.setVisibility(View.GONE);
                    //                    cb_choose.setChecked(false);
                    WaitDialog.ShowDialog(RegisterListNewActivity.this,"数据加载中....");
                    getRegisterList();
                    getStay();
                    break;
                default:
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


    /**
     * 获取区域
     */
    void getCity(String code) {
        OkHttpUtils
                .get()
                .url(InitApplication.newUrl + "system/regionList.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("region_code", code)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        WaitDialog.HideDialog();
                        //                                                Toast.makeText(AssistEntUpdateActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        //                        System.out.println(response);
                        WaitDialog.HideDialog();
                        CityBean cityBeann = JsonUtil.toObject(response, CityBean.class);
                        if (cityBeann.getCode() == 0) {
                            if (cityBeann.getData().size() > 0) {
                                mTitles.add("请选择");
                                mFragments.add(AreaPopRegisterNewFragment.newInstance(cityBeann.getData()));
                                initTablayout();
                            } else {
                                initTablayout();
                            }
                        } else {
                            Toast.makeText(RegisterListNewActivity.this, "获取数据失败，稍后再试", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    private void initTablayout() {
        ll_area_pop.setVisibility(View.VISIBLE);
        mFragmentAdapter = null;
        mFragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), mFragments, mTitles);
        mViewPager.setAdapter(mFragmentAdapter);//给ViewPager设置适配器
        mTabLayout.setupWithViewPager(mViewPager);//将TabLayout和ViewPager关联起来
        mViewPager.setCurrentItem(mFragments.size() - 1);
        mTabLayout.getTabAt(mFragments.size() - 1).select();
    }

    public void setUpdate(String name, String code) {
        //        ToastUtils.showToast(AreaActivity.this, code + name);
        //        ToastUtils.showToast(AreaActivity.this, mViewPager.getCurrentItem() + name + mTitles.size());
        List<String> tmp = new ArrayList<>();
        List<Fragment> tmpFragments = new ArrayList<>();
        for (int i = 0; i <= mViewPager.getCurrentItem(); i++) {
            tmp.add(mTitles.get(i));
            tmpFragments.add(mFragments.get(i));
        }
        tmp.add(tmp.size() - 1, name);
        tmp.remove(tmp.size() - 1);
        tmp.add("请选择");
        mTitles.clear();
        mTitles.addAll(tmp);
        mFragments.clear();
        mFragments.addAll(tmpFragments);
        //        resultCode = code;
        getCity(code);
    }


    /**
     * 获取待办
     */
    void getStay() {
        OkHttpUtils
                .get()
                .url(InitApplication.markUrl + "system/getMarkMatter.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("region_code", user.getKnowRegion())
                .addParams("realname", user.getRealname())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        tv_count.setText("暂无数据");
                        //                        Toast.makeText(PatrolListNewActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        //                        System.out.println(response);
                        stayMarkBean = JsonUtil.toObject(response, StayMarkBean.class);
                        if (stayMarkBean.getCode().equals("0")) {
                            SpannableString ss = new SpannableString("待审核" + String.valueOf(stayMarkBean.getData().getRegisterCount()) + "户");
                            ss.setSpan(new ForegroundColorSpan(Color.parseColor("#ff3d5d")), 3, 3 + String.valueOf(stayMarkBean.getData().getRegisterCount()).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            tv_count.setText(ss);
                        } else {
                            tv_count.setText("暂无数据");
                        }
                    }
                });
    }


    void postCheckRegister(String type, String ids, final String reason, String add_user, String region_code) {
        OkHttpUtils
                .post()
                .url(InitApplication.markUrl + "register/checkRegister.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("type", type)
                .addParams("ids", ids)
                .addParams("reason", reason)
                .addParams("add_user", add_user)
                .addParams("region_code", region_code)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        WaitDialog.HideDialog();
                        Toast.makeText(RegisterListNewActivity.this, "提交失败，稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        WaitDialog.HideDialog();
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            int code = jsonObject.optInt("code");
                            if (code == 0) {
                                Toast.makeText(RegisterListNewActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                                page = 1;
                                getRegisterList();
                                getStay();
                            } else {
                                Toast.makeText(RegisterListNewActivity.this, "提交失败，稍后再试", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

}
